public class OverridePESampleEdit
{
    public Boolean Submitted{get;set;} // submit flag
    public Boolean DisplaySave{get;set;} // display Save button or not
    public String IdSelect{get;set;}

    public String msg{get;set;}
    public PE_Sample_Management__c PESM{
         get{
             if(PESM== null)
                 PESM = [Select p.Year__c, p.InApproveProcess__c ,p.WOC_total_score__c, p.WOC_potential_score__c, p.WOC_final_weighting__c, p.WOC_budget_allocation_kg__c,
                 p.WOC_Total_budget_RMB__c, p.WOC_This_year_PE_Sample_kg__c, p.WOC_This_year_In_market_sales_kg__c, p.WOC_Last_year_sales_weighting__c, 
                 p.WOC_Last_year_conversion_weighting__c, p.WOC_Last_year_PE_Sample_kg__c, p.WOC_Last_year_In_market_sales_kg__c, p.WOCOver__c, 
                 p.WOCInMarketValueHide__c, p.WOCHighlight__c, p.WOCConversionForcast__c, p.Total_Budget_weighting__c, p.Total_Budget_allocation_kg__c, 
                 p.Total_Budget_Allocation_RMB__c, p.SOC_total_score__c, p.SOC_potential_score__c, p.SOC_final_weighting__c, 
                 p.SOC_budget_allocation_kg__c, p.SOC_Total_budget_RMB__c, p.SOC_This_year_PE_Sample_kg__c, p.SOC_This_year_In_market_sales_kg__c, 
                 p.SOC_Last_year_sales_weighting__c, p.SOC_Last_year_conversion_weighting__c, p.SOC_Last_year_PE_Sample_kg__c, p.SOC_Last_year_In_market_sales_kg__c, 
                 p.SOCOver__c, p.SOCInMarketValueHide__c, p.SOCHighlight__c, p.SOCConversionForcast__c, p.Rate__c, p.OwnerId, p.Name, p.NOC_total_score__c, 
                 p.NOC_potential_score__c, p.NOC_final_weighting__c, p.NOC_budget_allocation_kg__c, p.NOC_Total_budget_RMB__c, p.NOC_This_year_PE_Sample_kg__c, 
                 p.NOC_This_year_In_market_sales_kg__c, p.NOC_Last_year_sales_weighting__c, p.NOC_Last_year_conversion_weighting__c, p.NOC_Last_year_PE_Sample_kg__c, 
                 p.NOC_Last_year_In_market_sales_kg__c, p.NOCOver__c, p.NOCInMarketValueHide__c, p.NOCHighlight__c, p.NOCConversionForcast__c, 
                  p.Id, p.EOC_total_score__c, p.EOC_potential_score__c, p.EOC_final_weighting__c, 
                 p.EOC_budget_allocation_kg__c, p.EOC_Total_budget_RMB__c, p.EOC_This_year_PE_Sample_kg__c, p.EOC_This_year_In_market_sales_kg__c, 
                 p.EOC_Last_year_sales_weighting__c, p.EOC_Last_year_conversion_weighting__c, p.EOC_Last_year_PE_Sample_kg__c, p.EOC_Last_year_In_market_sales_kg__c, 
                 p.EOCOver__c, p.EOCInMarketValueHide__c, p.EOCHighlight__c, p.EOCConversionForcast__c, p.Deal__c,  
                 p.Budget_hold_in_HQ_weighting__c, p.Budget_hold_in_HQ_RMB__c, p.Budget_hold_in_HQ_Budget_allocation_kg__c, p.Budget_auto_top_down_to_region_weighting__c, 
                 p.Budget_auto_top_down_to_region_RMB__c, p.Budget_auto_top_down_to_region_Budget_kg__c From PE_Sample_Management__c p Where p.id =: UrlId];
             return PESM;
         }
         set;}  
         
    public List<TableItemControl> TableItemsUp{
         get{
             if(TableItemsUp == null)
                 TableItemsUp = new List<TableItemControl>();
             return TableItemsUp;
         }
        set;}
        
    public List<TableItemControl> TableItemsDown{         
        get{
             if(TableItemsDown== null)
                 TableItemsDown= new List<TableItemControl>();
             return TableItemsDown;
         }
        set;}    
    
    public String UrlId
    {
        get{        
            return System.currentPageReference().getParameters().get('id');
        }  
        set;
    }  
 
    public OverridePESampleEdit(ApexPages.StandardController controller){
        this.controller=controller;
        
        Submitted = true;
        DisplaySave = true;
        if(PESM.Deal__c == true){
            msg = '样品已下发，不能再做修改!';
            Submitted = false;
            DisplaySave = false;            
        }
        if(PESM.InApproveProcess__c == true){
            msg = '样品已进入审批，不能再做修改!';
            Submitted = false;
            DisplaySave = false;            
        }        
        
        TableItemControl T1 = new TableItemControl();
        T1.Str = '总预算';   
        T1.Str1 = '%';       
        TableItemControl T2 = new TableItemControl();
        T2.Str = '保留在总部';
        T2.Str1 = '%';   
        TableItemControl T3 = new TableItemControl();
        T3.Str = '分配到大区';
        T3.Str1 = '%';   
        TableItemsUp.add(T1);
        TableItemsUp.add(T2);
        TableItemsUp.add(T3);
        TableItemsUp[0].PE.Total_Budget_weighting__c = PESM.Total_Budget_weighting__c ;
        TableItemsUp[0].PE.Total_Budget_allocation_kg__c = PESM.Total_Budget_allocation_kg__c;
        TableItemsUp[0].PE.Total_Budget_Allocation_RMB__c = PESM.Total_Budget_Allocation_RMB__c; 
        TableItemsUp[1].PE.Total_Budget_weighting__c = PESM.Budget_hold_in_HQ_weighting__c;
        TableItemsUp[1].PE.Total_Budget_allocation_kg__c = PESM.Budget_hold_in_HQ_Budget_allocation_kg__c;
        TableItemsUp[1].PE.Total_Budget_Allocation_RMB__c = PESM.Budget_hold_in_HQ_RMB__c; 
        TableItemsUp[2].PE.Total_Budget_weighting__c = PESM.Budget_auto_top_down_to_region_weighting__c;
        TableItemsUp[2].PE.Total_Budget_allocation_kg__c = PESM.Budget_auto_top_down_to_region_Budget_kg__c;    
        TableItemsUp[2].PE.Total_Budget_Allocation_RMB__c = PESM.Budget_auto_top_down_to_region_RMB__c;
        
        List<Place__c> pList = [Select id,name From Place__c Where PlaceType__c = 'Region'];
        for(Place__c p : pList)
        {
            TableItemControl T = new TableItemControl();
            T.Str = p.name;
            TableItemsDown.add(T);
        }
                       
        TableItemsDown[0].PE.SOC_Last_year_sales_weighting__c = PESM.EOC_Last_year_sales_weighting__c;
        TableItemsDown[0].PE.SOC_Last_year_conversion_weighting__c = PESM.EOC_Last_year_conversion_weighting__c;
        TableItemsDown[0].PE.SOC_potential_score__c = PESM.EOC_potential_score__c;
        TableItemsDown[0].PE.SOC_total_score__c = PESM.EOC_total_score__c;
        TableItemsDown[0].PE.SOC_final_weighting__c = PESM.EOC_final_weighting__c;
        TableItemsDown[0].PE.SOC_budget_allocation_kg__c = PESM.EOC_budget_allocation_kg__c;
        TableItemsDown[0].PE.SOC_Last_year_In_market_sales_kg__c = PESM.EOC_Last_year_In_market_sales_kg__c;
        TableItemsDown[0].PE.SOC_Last_year_PE_Sample_kg__c = PESM.EOC_Last_year_PE_Sample_kg__c;
        TableItemsDown[0].PE.SOC_This_year_In_market_sales_kg__c = PESM.EOC_This_year_In_market_sales_kg__c;
        TableItemsDown[0].PE.SOC_This_year_PE_Sample_kg__c = PESM.EOC_This_year_PE_Sample_kg__c;
        TableItemsDown[0].PE.SOC_Total_budget_RMB__c = PESM.EOC_Total_budget_RMB__c;
        TableItemsDown[0].PE.SOCInMarketValueHide__c = PESM.EOCInMarketValueHide__c;
        TableItemsDown[0].PE.SOCConversionForcast__c = PESM.EOCConversionForcast__c;

        TableItemsDown[1].PE.SOC_Last_year_sales_weighting__c = PESM.NOC_Last_year_sales_weighting__c;
        TableItemsDown[1].PE.SOC_Last_year_conversion_weighting__c = PESM.NOC_Last_year_conversion_weighting__c;
        TableItemsDown[1].PE.SOC_potential_score__c = PESM.NOC_potential_score__c;
        TableItemsDown[1].PE.SOC_total_score__c = PESM.NOC_total_score__c;
        TableItemsDown[1].PE.SOC_final_weighting__c = PESM.NOC_final_weighting__c;
        TableItemsDown[1].PE.SOC_budget_allocation_kg__c = PESM.NOC_budget_allocation_kg__c;
        TableItemsDown[1].PE.SOC_Last_year_In_market_sales_kg__c = PESM.NOC_Last_year_In_market_sales_kg__c;
        TableItemsDown[1].PE.SOC_Last_year_PE_Sample_kg__c = PESM.NOC_Last_year_PE_Sample_kg__c;
        TableItemsDown[1].PE.SOC_This_year_In_market_sales_kg__c = PESM.NOC_This_year_In_market_sales_kg__c;
        TableItemsDown[1].PE.SOC_This_year_PE_Sample_kg__c = PESM.NOC_This_year_PE_Sample_kg__c;
        TableItemsDown[1].PE.SOC_Total_budget_RMB__c = PESM.NOC_Total_budget_RMB__c;
        TableItemsDown[1].PE.SOCInMarketValueHide__c = PESM.NOCInMarketValueHide__c;
        TableItemsDown[1].PE.SOCConversionForcast__c = PESM.NOCConversionForcast__c;
               
        TableItemsDown[2].PE.SOC_Last_year_sales_weighting__c = PESM.SOC_Last_year_sales_weighting__c;
        TableItemsDown[2].PE.SOC_Last_year_conversion_weighting__c = PESM.SOC_Last_year_conversion_weighting__c;
        TableItemsDown[2].PE.SOC_potential_score__c = PESM.SOC_potential_score__c;
        TableItemsDown[2].PE.SOC_total_score__c = PESM.SOC_total_score__c;
        TableItemsDown[2].PE.SOC_final_weighting__c = PESM.SOC_final_weighting__c;
        TableItemsDown[2].PE.SOC_budget_allocation_kg__c = PESM.SOC_budget_allocation_kg__c;
        TableItemsDown[2].PE.SOC_Last_year_In_market_sales_kg__c =  PESM.SOC_Last_year_In_market_sales_kg__c;
        TableItemsDown[2].PE.SOC_Last_year_PE_Sample_kg__c = PESM.SOC_Last_year_PE_Sample_kg__c;
        TableItemsDown[2].PE.SOC_This_year_In_market_sales_kg__c = PESM.SOC_This_year_In_market_sales_kg__c;
        TableItemsDown[2].PE.SOC_This_year_PE_Sample_kg__c = PESM.SOC_This_year_PE_Sample_kg__c;
        TableItemsDown[2].PE.SOC_Total_budget_RMB__c = PESM.SOC_Total_budget_RMB__c;
        TableItemsDown[2].PE.SOCInMarketValueHide__c = PESM.SOCInMarketValueHide__c;
        TableItemsDown[2].PE.SOCConversionForcast__c = PESM.SOCConversionForcast__c;        
        
        TableItemsDown[3].PE.SOC_Last_year_sales_weighting__c = PESM.WOC_Last_year_sales_weighting__c;
        TableItemsDown[3].PE.SOC_Last_year_conversion_weighting__c = PESM.WOC_Last_year_conversion_weighting__c;
        TableItemsDown[3].PE.SOC_potential_score__c = PESM.WOC_potential_score__c;
        TableItemsDown[3].PE.SOC_total_score__c = PESM.WOC_total_score__c;
        TableItemsDown[3].PE.SOC_final_weighting__c = PESM.WOC_final_weighting__c;
        TableItemsDown[3].PE.SOC_budget_allocation_kg__c = PESM.WOC_budget_allocation_kg__c;
        TableItemsDown[3].PE.SOC_Last_year_In_market_sales_kg__c = PESM.WOC_Last_year_In_market_sales_kg__c;
        TableItemsDown[3].PE.SOC_Last_year_PE_Sample_kg__c = PESM.WOC_Last_year_PE_Sample_kg__c;
        TableItemsDown[3].PE.SOC_This_year_In_market_sales_kg__c = PESM.WOC_This_year_In_market_sales_kg__c;
        TableItemsDown[3].PE.SOC_This_year_PE_Sample_kg__c = PESM.WOC_This_year_PE_Sample_kg__c;
        TableItemsDown[3].PE.SOC_Total_budget_RMB__c = PESM.WOC_Total_budget_RMB__c;
        TableItemsDown[3].PE.SOCInMarketValueHide__c = PESM.WOCInMarketValueHide__c;
        TableItemsDown[3].PE.SOCConversionForcast__c = PESM.WOCConversionForcast__c;
        
        TableItemControl TT = new TableItemControl();
        TT.Str = '总计';
        TT.InputPercent = 100;
        TT.PE.SOC_Last_year_sales_weighting__c = 100;
        TT.PE.SOC_Last_year_conversion_weighting__c = 100;        
        TT.PE.SOC_budget_allocation_kg__c = TableItemsDown[0].PE.SOC_budget_allocation_kg__c + TableItemsDown[1].PE.SOC_budget_allocation_kg__c + TableItemsDown[2].PE.SOC_budget_allocation_kg__c+ TableItemsDown[3].PE.SOC_budget_allocation_kg__c;
        TT.PE.SOC_Total_budget_RMB__c = TableItemsDown[0].PE.SOC_Total_budget_RMB__c + TableItemsDown[1].PE.SOC_Total_budget_RMB__c +TableItemsDown[2].PE.SOC_Total_budget_RMB__c +TableItemsDown[3].PE.SOC_Total_budget_RMB__c;
        TT.PE.SOCInMarketValueHide__c = TableItemsDown[0].PE.SOCInMarketValueHide__c + TableItemsDown[1].PE.SOCInMarketValueHide__c + TableItemsDown[2].PE.SOCInMarketValueHide__c + TableItemsDown[3].PE.SOCInMarketValueHide__c;
        TT.PE.SOCConversionForcast__c = TableItemsDown[0].PE.SOCConversionForcast__c + TableItemsDown[1].PE.SOCConversionForcast__c + TableItemsDown[2].PE.SOCConversionForcast__c + TableItemsDown[3].PE.SOCConversionForcast__c;
        
        Decimal TotalLastYearPEShipmentValue = 0;
        Decimal TotalThisYearPEShipmentValue = 0;
        Decimal TotalLastYearPEConversionValue = 0;
        Decimal TotalThisYearPEConversionValue = 0;
        Decimal TotalLastYearInMarketSalesValue = 0;
        Decimal TotalThisYearInMarketSalesValue = 0;
        
        List<AggregateResult> TotalLastYearInMarketSales = [Select sum(InMarketSaleskg__c)TotalIn From InMarketSales__c Where InMarketSaleskg__c > 0 and Year__c =: Integer.valueof(PESM.Year__c)-1 ];
        List<AggregateResult> TotalThisYearInMarketSales = [Select sum(InMarketSalesForcast__c)TotalIn From InMarketSales__c Where InMarketSaleskg__c > 0 and Year__c =: Integer.valueof(PESM.Year__c)];        
        
        List<AggregateResult> TotalLastYearPEConversion = [Select sum(PE_conversion_achieve__c)TotalIn  From Achieve__c Where PE_conversion_achieve__c > 0 and   Year__c =: string.valueof(Integer.valueof(PESM.Year__c)-1)];
        List<AggregateResult> TotalThisYearPEConversion = [Select sum(PE_Conversion_forecast__c)TotalIn  From Achieve__c Where PE_conversion_achieve__c > 0 and   Year__c =: PESM.Year__c];
        
        List<AggregateResult> TotalLastYearPEShipment = [Select sum(KG__c)TotalIn  From Shipment__c Where KG__c > 0 and   Year__c =: Integer.valueof(PESM.Year__c)-1 and CalcInKg__c =: true];

        if((Decimal)TotalLastYearPEShipment[0].get('TotalIn') != null)
            TotalLastYearPEShipmentValue = (Decimal)TotalLastYearPEShipment[0].get('TotalIn');

        TotalThisYearPEShipmentValue = TT.PE.SOC_budget_allocation_kg__c;
        if((Decimal)TotalLastYearPEConversion[0].get('TotalIn') != null)     
            TotalLastYearPEConversionValue = (Decimal)TotalLastYearPEConversion[0].get('TotalIn');
        if((Decimal)TotalThisYearPEConversion[0].get('TotalIn') != null)     
            TotalThisYearPEConversionValue = (Decimal)TotalThisYearPEConversion[0].get('TotalIn');
        if((Decimal)TotalLastYearInMarketSales[0].get('TotalIn') != null)        
            TotalLastYearInMarketSalesValue = (Decimal)TotalLastYearInMarketSales[0].get('TotalIn');
        if((Decimal)TotalThisYearInMarketSales[0].get('TotalIn') != null)      
            TotalThisYearInMarketSalesValue = (Decimal)TotalThisYearInMarketSales[0].get('TotalIn');
   
        
        if(TotalLastYearPEShipmentValue > 0) 
            TT.PE.SOC_Last_year_In_market_sales_kg__c = TotalLastYearInMarketSalesValue / TotalLastYearPEShipmentValue ;
        else
            TT.PE.SOC_Last_year_In_market_sales_kg__c = 0;
  
        if(TotalLastYearPEConversionValue > 0) 
            TT.PE.SOC_Last_year_PE_Sample_kg__c = TotalLastYearPEShipmentValue / TotalLastYearPEConversionValue ;
        else
            TT.PE.SOC_Last_year_PE_Sample_kg__c = 0;
   
        if(TotalThisYearPEShipmentValue > 0) 
            TT.PE.SOC_This_year_In_market_sales_kg__c = TotalThisYearInMarketSalesValue / TotalThisYearPEShipmentValue ;
        else
            TT.PE.SOC_This_year_In_market_sales_kg__c = 0;
        if(TotalThisYearPEConversionValue > 0)    
            TT.PE.SOC_This_year_PE_Sample_kg__c = TotalThisYearPEShipmentValue / TotalThisYearPEConversionValue ;
        else
            TT.PE.SOC_This_year_PE_Sample_kg__c = 0;
   
        TableItemsDown.add(TT);           
                
    }

    public void SetRecord(Boolean deal)
    {
        // set table items up
        PESM.Total_Budget_weighting__c = TableItemsUp[0].PE.Total_Budget_weighting__c;
        PESM.Total_Budget_allocation_kg__c = TableItemsUp[0].PE.Total_Budget_allocation_kg__c;
        PESM.Total_Budget_Allocation_RMB__c = TableItemsUp[0].PE.Total_Budget_allocation_kg__c * PESM.Rate__c; 
        PESM.Budget_hold_in_HQ_weighting__c = TableItemsUp[1].PE.Total_Budget_weighting__c;
        PESM.Budget_hold_in_HQ_Budget_allocation_kg__c = TableItemsUp[1].PE.Total_Budget_allocation_kg__c;
        PESM.Budget_hold_in_HQ_RMB__c = TableItemsUp[1].PE.Total_Budget_allocation_kg__c * PESM.Rate__c; 
        PESM.Budget_auto_top_down_to_region_weighting__c = TableItemsUp[2].PE.Total_Budget_weighting__c;
        PESM.Budget_auto_top_down_to_region_Budget_kg__c = TableItemsUp[2].PE.Total_Budget_allocation_kg__c;    
        PESM.Budget_auto_top_down_to_region_RMB__c = TableItemsUp[2].PE.Total_Budget_allocation_kg__c * PESM.Rate__c;

        //set table items down
        PESM.EOC_Last_year_sales_weighting__c = TableItemsDown[0].PE.SOC_Last_year_sales_weighting__c;
        PESM.EOC_Last_year_conversion_weighting__c = TableItemsDown[0].PE.SOC_Last_year_conversion_weighting__c;
        PESM.EOC_potential_score__c = TableItemsDown[0].PE.SOC_potential_score__c;
        PESM.EOC_total_score__c = TableItemsDown[0].PE.SOC_total_score__c;
        PESM.EOC_final_weighting__c = TableItemsDown[0].PE.SOC_final_weighting__c;
        PESM.EOC_budget_allocation_kg__c = TableItemsDown[0].PE.SOC_budget_allocation_kg__c;
       
        
        PESM.EOC_Last_year_In_market_sales_kg__c = TableItemsDown[0].PE.SOC_Last_year_In_market_sales_kg__c;
        PESM.EOC_Last_year_PE_Sample_kg__c = TableItemsDown[0].PE.SOC_Last_year_PE_Sample_kg__c;
        if(PESM.EOC_budget_allocation_kg__c > 0)
            PESM.EOC_This_year_In_market_sales_kg__c = PESM.EOCInMarketValueHide__c / PESM.EOC_budget_allocation_kg__c;
        else
            PESM.EOC_This_year_In_market_sales_kg__c = 0;
        PESM.EOC_This_year_In_market_sales_kg__c = PESM.EOC_This_year_In_market_sales_kg__c.intValue();
        if(PESM.EOCConversionForcast__c > 0)
            PESM.EOC_This_year_PE_Sample_kg__c = PESM.EOC_budget_allocation_kg__c / PESM.EOCConversionForcast__c;
        else
            PESM.EOC_This_year_PE_Sample_kg__c = 0;
        PESM.EOC_This_year_PE_Sample_kg__c = PESM.EOC_This_year_PE_Sample_kg__c.intValue();
        PESM.EOC_Total_budget_RMB__c = TableItemsDown[0].PE.SOC_budget_allocation_kg__c * PESM.Rate__c;

        PESM.NOC_Last_year_sales_weighting__c = TableItemsDown[1].PE.SOC_Last_year_sales_weighting__c;
        PESM.NOC_Last_year_conversion_weighting__c = TableItemsDown[1].PE.SOC_Last_year_conversion_weighting__c;
        PESM.NOC_potential_score__c = TableItemsDown[1].PE.SOC_potential_score__c;
        PESM.NOC_total_score__c = TableItemsDown[1].PE.SOC_total_score__c;
        PESM.NOC_final_weighting__c = TableItemsDown[1].PE.SOC_final_weighting__c;
        PESM.NOC_budget_allocation_kg__c = TableItemsDown[1].PE.SOC_budget_allocation_kg__c;
      
        PESM.NOC_Last_year_In_market_sales_kg__c = TableItemsDown[1].PE.SOC_Last_year_In_market_sales_kg__c;
        PESM.NOC_Last_year_PE_Sample_kg__c = TableItemsDown[1].PE.SOC_Last_year_PE_Sample_kg__c;
        if(PESM.NOC_budget_allocation_kg__c > 0)
            PESM.NOC_This_year_In_market_sales_kg__c = PESM.NOCInMarketValueHide__c / PESM.NOC_budget_allocation_kg__c;
        else
            PESM.NOC_This_year_In_market_sales_kg__c = 0;
        PESM.NOC_This_year_In_market_sales_kg__c = PESM.NOC_This_year_In_market_sales_kg__c.intValue();
        if(PESM.NOCConversionForcast__c > 0)
            PESM.NOC_This_year_PE_Sample_kg__c = PESM.NOC_budget_allocation_kg__c / PESM.NOCConversionForcast__c;
        else
            PESM.NOC_This_year_PE_Sample_kg__c = 0;
        PESM.NOC_This_year_PE_Sample_kg__c = PESM.NOC_This_year_PE_Sample_kg__c.intValue();
        PESM.NOC_Total_budget_RMB__c = TableItemsDown[1].PE.SOC_budget_allocation_kg__c * PESM.Rate__c;
       
        PESM.SOC_Last_year_sales_weighting__c = TableItemsDown[2].PE.SOC_Last_year_sales_weighting__c;
        PESM.SOC_Last_year_conversion_weighting__c = TableItemsDown[2].PE.SOC_Last_year_conversion_weighting__c;
        PESM.SOC_potential_score__c = TableItemsDown[2].PE.SOC_potential_score__c;
        PESM.SOC_total_score__c = TableItemsDown[2].PE.SOC_total_score__c;
        PESM.SOC_final_weighting__c = TableItemsDown[2].PE.SOC_final_weighting__c;
        PESM.SOC_budget_allocation_kg__c = TableItemsDown[2].PE.SOC_budget_allocation_kg__c;
        
        PESM.SOC_Last_year_In_market_sales_kg__c = TableItemsDown[2].PE.SOC_Last_year_In_market_sales_kg__c;
        PESM.SOC_Last_year_PE_Sample_kg__c = TableItemsDown[2].PE.SOC_Last_year_PE_Sample_kg__c;
        if(PESM.SOC_budget_allocation_kg__c > 0)
            PESM.SOC_This_year_In_market_sales_kg__c = PESM.SOCInMarketValueHide__c / PESM.SOC_budget_allocation_kg__c;
        else
            PESM.SOC_This_year_In_market_sales_kg__c = 0;
        PESM.SOC_This_year_In_market_sales_kg__c = PESM.SOC_This_year_In_market_sales_kg__c.intValue();
        if(PESM.SOCConversionForcast__c > 0)
            PESM.SOC_This_year_PE_Sample_kg__c = PESM.SOC_budget_allocation_kg__c / PESM.SOCConversionForcast__c;
        else
            PESM.SOC_This_year_PE_Sample_kg__c = 0;
        PESM.SOC_This_year_PE_Sample_kg__c = PESM.SOC_This_year_PE_Sample_kg__c.intValue();
        PESM.SOC_Total_budget_RMB__c = TableItemsDown[2].PE.SOC_budget_allocation_kg__c * PESM.Rate__c;
        
        PESM.WOC_Last_year_sales_weighting__c = TableItemsDown[3].PE.SOC_Last_year_sales_weighting__c;
        PESM.WOC_Last_year_conversion_weighting__c = TableItemsDown[3].PE.SOC_Last_year_conversion_weighting__c;
        PESM.WOC_potential_score__c = TableItemsDown[3].PE.SOC_potential_score__c;
        PESM.WOC_total_score__c = TableItemsDown[3].PE.SOC_total_score__c;
        PESM.WOC_final_weighting__c = TableItemsDown[3].PE.SOC_final_weighting__c;
        PESM.WOC_budget_allocation_kg__c = TableItemsDown[3].PE.SOC_budget_allocation_kg__c;
       
        PESM.WOC_Last_year_In_market_sales_kg__c = TableItemsDown[3].PE.SOC_Last_year_In_market_sales_kg__c;
        PESM.WOC_Last_year_PE_Sample_kg__c = TableItemsDown[3].PE.SOC_Last_year_PE_Sample_kg__c;
        if(PESM.WOC_budget_allocation_kg__c > 0)
            PESM.WOC_This_year_In_market_sales_kg__c = PESM.WOCInMarketValueHide__c / PESM.WOC_budget_allocation_kg__c;
        else
            PESM.WOC_This_year_In_market_sales_kg__c = 0;
        PESM.WOC_This_year_In_market_sales_kg__c = PESM.WOC_This_year_In_market_sales_kg__c.intValue();
        if(PESM.WOCConversionForcast__c > 0)
            PESM.WOC_This_year_PE_Sample_kg__c = PESM.WOC_budget_allocation_kg__c / PESM.WOCConversionForcast__c;
        else
            PESM.WOC_This_year_PE_Sample_kg__c = 0;
        PESM.WOC_This_year_PE_Sample_kg__c = PESM.WOC_This_year_PE_Sample_kg__c.intValue();
        PESM.WOC_Total_budget_RMB__c = TableItemsDown[3].PE.SOC_budget_allocation_kg__c * PESM.Rate__c;
     
        PESM.Deal__c = Deal;
        PESM.name = PESM.Year__c  + '年' + '专业样品管理';
        update PESM;    
    
    }


    public PageReference Save()
    {
  
        Decimal AllocatedNum;
        
        if(TableItemsDown[0].PE.SOC_budget_allocation_kg__c == null)
        {
            TableItemsDown[0].PE.SOC_budget_allocation_kg__c = 0;
        }
        if(TableItemsDown[1].PE.SOC_budget_allocation_kg__c == null)
        {
            TableItemsDown[1].PE.SOC_budget_allocation_kg__c = 0;
        }
        if(TableItemsDown[2].PE.SOC_budget_allocation_kg__c == null)
        {
            TableItemsDown[2].PE.SOC_budget_allocation_kg__c = 0;
        }
        if(TableItemsDown[3].PE.SOC_budget_allocation_kg__c == null)
        {
            TableItemsDown[3].PE.SOC_budget_allocation_kg__c = 0;
        }                        
        AllocatedNum = TableItemsDown[0].PE.SOC_budget_allocation_kg__c + TableItemsDown[1].PE.SOC_budget_allocation_kg__c 
                        + TableItemsDown[2].PE.SOC_budget_allocation_kg__c + TableItemsDown[3].PE.SOC_budget_allocation_kg__c;
        
        if(AllocatedNum != TableItemsUp[2].PE.Total_Budget_allocation_kg__c)
        {
            msg = '分配公斤数应等于分配到大区的预算公斤数';
            return null;
        }    
    
        SetRecord(false);
                
        PageReference pr=new PageReference('/'+UrlId);
        pr.setRedirect(true);
        return pr;
    }

    public PageReference Deal()
    {
        SetRecord(true);
        
        PageReference pr=new PageReference('/'+UrlId);
        pr.setRedirect(true);
        return pr;
    }
            
    public PageReference Cancel()
    {
        PageReference pr=new PageReference('/'+UrlId);
        pr.setRedirect(true);
        return pr;
    }
    private final ApexPages.StandardController controller;
    
    static testMethod void OverrideViewTest()
    {
        Profile p  = [select id from profile where name = 'System Administrator'or name =: '系统管理员'];
        
        User u1 = new User (alias = 'standt', email = 'shuaicao@isoftstone.com', emailencodingkey='UTF-8', 
                lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', Title__c = 'Sales Rep',profileid = p.id, 
                timezonesidkey = 'America/Los_Angeles', username='asdfdaa@testorg1.com');
        insert u1;
        
        System.runAs(u1) {
            PE_Sample_Management__c PE = new PE_Sample_Management__c();
            PE.WOC_Total_budget_RMB__c = 1;
            PE.WOC_budget_allocation_kg__c = 2;
            PE.WOCInMarketValueHide__c = 0;
            PE.WOCConversionForcast__c = 0;                  
            PE.SOC_Total_budget_RMB__c = 0;
            PE.SOC_budget_allocation_kg__c = 0;
            PE.SOCInMarketValueHide__c = 0;
            PE.SOCConversionForcast__c = 0;                  
            PE.EOC_Total_budget_RMB__c = 0;
            PE.EOC_budget_allocation_kg__c = 0;
            PE.EOCInMarketValueHide__c = 0;
            PE.EOCConversionForcast__c = 0;            
            PE.NOC_Total_budget_RMB__c = 0;
            PE.NOC_budget_allocation_kg__c = 0;
            PE.NOCInMarketValueHide__c = 0;
            PE.NOCConversionForcast__c = 0;
            PE.Rate__c =2;
            PE.Total_Budget_Allocation_RMB__c = 1;
            insert PE;
            test.setCurrentPageReference(Page.OverridePESampleEdit);
            System.currentPageReference().getParameters().put('id', PE.Id);             
            PE.ownerid = u1.id;
            ApexPages.StandardController AP=new ApexPages.StandardController(PE );
            OverridePESampleEdit testCase = new OverridePESampleEdit(AP);
            testCase.PESM = new PE_Sample_Management__c();
            testCase.TableItemsUp[0].PE.Total_Budget_allocation_kg__c = 1;
            testCase.PESM.Rate__c = 1;
            testCase.TableItemsUp[1].PE.Total_Budget_allocation_kg__c = 1;
            testCase.TableItemsUp[2].PE.Total_Budget_allocation_kg__c = 1;
            testCase.PESM.EOCConversionForcast__c = 1;
            testCase.PESM = PE;

            testCase.Save();
            testCase.Cancel();
            testCase.Deal();
        
        }        
    }
 
}